package com.maigao.api.controller;

import com.maigao.api.interceptors.identify.Identify;
import com.maigao.entity.ResultJson;
import com.maigao.entity.enums.ResultStatus;
import com.maigao.entity.form.AddStudentForm;
import com.maigao.entity.form.SearchStudentForm;
import com.maigao.entity.vo.NewlyAddedStudentVo;
import com.maigao.entity.vo.StudentVO;
import com.maigao.service.StudentService;
import com.maigao.util.CollectionUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.tuple.Triple;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: maigao
 * @description:
 * @author: yongza
 * @create: 2024-10-21 22:57
 **/
@RestController
@RequestMapping(value = "/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @Identify
    @ResponseBody
    @RequestMapping("listAll")
    public ResultJson<List<StudentVO>> listAll() {
        List<StudentVO> students = studentService.tryFindAll();
        if (CollectionUtils.isEmpty(students)) {
            return new ResultJson<>(ResultStatus.NO_DATA);
        }
        return ResultJson.success(students);
    }

    @Identify
    @ResponseBody
    @RequestMapping("add")
    public ResultJson addStudent(AddStudentForm addStudentForm) {
        Triple<Boolean, String, StudentVO> addResult = studentService.tryAddStudent(addStudentForm);
        if (addResult.getLeft()) {
            return ResultJson.success(addResult.getRight());
        }
        return ResultJson.fail(addResult.getMiddle());
    }

    @Identify
    @ResponseBody
    @RequestMapping("find")
    public ResultJson findStudent(SearchStudentForm searchStudentForm) {
        Triple<Boolean, StudentVO, String> searchResult = studentService.tryFindStudent(searchStudentForm);
        if (searchResult.getLeft()) {
            return ResultJson.success(searchResult.getMiddle());
        }
        return ResultJson.fail(searchResult.getRight());
    }

    @Identify
    @ResponseBody
    @RequestMapping("search")
    public ResultJson searchStudent(@RequestParam String userName) {
        Triple<Boolean, List<StudentVO>, String> searchResult = studentService.trySearchStudent(userName);
        if (searchResult.getLeft()) {
            return ResultJson.success(searchResult.getMiddle());
        }
        return ResultJson.fail(searchResult.getRight());
    }

    @Identify
    @ResponseBody
    @RequestMapping("modifyStatus")
    public ResultJson modifyStudentStatus(@RequestParam Integer studentId, @RequestParam Integer newStatus) {
        Pair<Boolean, String> modifyResult = studentService.tryUpdateStatus(studentId, newStatus);
        if (modifyResult.getLeft()) {
            return ResultJson.success("");
        }
        return ResultJson.fail(modifyResult.getRight());
    }

    @Identify
    @ResponseBody
    @RequestMapping("modifyCoach")
    public ResultJson modifyStudentCoach(@RequestParam Integer studentId, @RequestParam Integer coachId) {
        Pair<Boolean, String> searchResult = studentService.tryUpdateCoach(studentId, coachId);
        if (searchResult.getLeft()) {
            return ResultJson.success(searchResult.getRight());
        }
        return ResultJson.fail(searchResult.getRight());
    }

    @Identify
    @ResponseBody
    @RequestMapping("findNewlyAddedStudent")
    public ResultJson findNewlyAddedStudent(@RequestParam String fromTime, @RequestParam String endTime) {
        Triple<Boolean, String, NewlyAddedStudentVo> searchResult = studentService.findStudentByTimeRange(fromTime, endTime);
        if (searchResult.getLeft()) {
            return ResultJson.success(searchResult.getRight());
        }
        return ResultJson.fail(searchResult.getMiddle());
    }

}
